package job.manager.db.sb;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import job.manager.db.dao.ProfilesDAO;
import job.manager.db.dao.TimeReportDAO;
import job.manager.db.dao.TimeScheduleDAO;
import job.manager.db.dao.UserDAO;
import job.manager.db.ec.ProfilesEB;
import job.manager.db.ec.TimeReportEB;
import job.manager.db.ec.TimeScheduleEB;
import job.manager.db.ec.TimeScheduleRowEB;
import job.manager.db.ec.UsersEB;

/**
 *  SessionBean is the top layer and the main access point for handling the communcation
 *  from other modules to the database.
 *  This Bean is handling all the time request, all the schudel and the timereport handling
 *
 */
@Stateless
public class TimeBean implements TimeRemote, TimeLocal {

    @PersistenceContext
    EntityManager em;

    public TimeReportEB getTimeReport(Integer pk) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.getTimeReport(pk);
    }

    public java.util.Collection<TimeReportEB> getTimeReportByJob(Integer jobbId) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.getAllTimeReportJob(jobbId);
    }

    public java.util.Collection<TimeReportEB> getTimeReportByProfile(Integer profileId) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.getAllTimeReportWorker(em.find(ProfilesEB.class, profileId));
    }
    public java.util.Collection<TimeReportEB> getTimeReportByProfileDateSorted(Integer profileId) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.getAllTimeReportWorkerSortedByDate(em.find(ProfilesEB.class, profileId));
    }

    public java.util.Collection<TimeReportEB> getAllTimeReport() {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.getAllTimeReport();
    }

    public int update(TimeReportEB report) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.update(report);
    }

    public TimeReportEB insert(TimeReportEB report) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.insert(report);
    }

    public int delete(TimeReportEB report) {
        TimeReportDAO dao = new TimeReportDAO(em);
        return dao.delete(report);
    }

    /***************
     * This part has all the functions for the timeschedule
     * TimeSchedule
     */
    public TimeScheduleEB getTimeSchedule(Integer pk) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.getTimeSchedule(pk);
    }

    public java.util.Collection<TimeScheduleEB> getAllTimeSchedule() {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.getAllTimeSchedule();
    }

    public int update(TimeScheduleEB report) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.update(report);
    }

    public TimeScheduleEB insert(TimeScheduleEB report) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.insert(report);
    }

    public int delete(TimeScheduleEB report) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.delete(report);
    }
    /***************
     * this part has all the functions for handling the timeSheculdeRow
     * TimeScheduleRow
     */
    public TimeScheduleRowEB getTimeScheduleRow(Integer pk) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.getTimeScheduleRow(pk);
    }

    public java.util.Collection<TimeScheduleRowEB> getAllTimeScheduleRow() {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.getAllTimeScheduleRow();
    }

    public int update(TimeScheduleRowEB report) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.update(report);
    }

    public TimeScheduleRowEB insert(TimeScheduleRowEB report) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.insert(report);
    }

    public int delete(TimeScheduleRowEB report) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        return dao.delete(report);
    }

    public ArrayList<TimeScheduleRowEB> getTimeReportsByUserNameWS(String username) {
        TimeScheduleDAO dao = new TimeScheduleDAO(em);
        ProfilesDAO pDAO = new ProfilesDAO(em);
        UserDAO uDAO = new UserDAO(em);
        TimeReportDAO tDAO = new TimeReportDAO(em);

        UsersEB user = uDAO.getUserByUsername(username);
        ProfilesEB profile = pDAO.getProfile(user.getProfileId());
        Collection<TimeReportEB> reports = tDAO.getAllTimeReportWorker(profile);
        ArrayList<TimeScheduleRowEB> retList = new ArrayList<TimeScheduleRowEB>();
        for(TimeReportEB r : reports)
        {
            TimeScheduleRowEB rowItem = new TimeScheduleRowEB();
            rowItem.setId(r.getScheduleRowId().getId());
            rowItem.setDateTime(r.getScheduleRowId().getDateTime());
            rowItem.setComment(r.getScheduleRowId().getComment());
            retList.add(rowItem);
        }
        return retList;
    }
}
